<?php
namespace filter\auth;


use core\Application;
use core\controller\Controller;
use core\filter\Filter;
use core\library\Input;
use core\utils\BeanManager;

class AuthFilter extends Filter
{
    protected $module = 'admin';

    private $white = [
        ['controller'=>'login', 'action'=>'*']
    ];

    /**
     * 进行访问权限校验
     */
    function before() {
        $app = BeanManager::getInstance()->getBean(Application::class);
        $controller = $app->getController();
        $action = $app->getAction();
        if (!$this->isWhite($controller, $action)) {
            if (!BeanManager::getInstance()->getBean(Input::class)->session('userid')) {
                (new Controller())->redirect('/admin/login/login');
            }
        }
    }

    private function isWhite($controller, $actoin) {
        foreach ($this->white as $r) {
            if ( ($controller==$r['controller'] || $r['controller']=='*') &&
                ($actoin==$r['action'] || $r['action'] == '*'))
                return true;
        }
        return false;
    }

    /**
     * finally
     * @return mixed
     */
    function after()
    {
        // TODO: Implement after() method.
    }

    function afterThrowing()
    {
        // TODO: Implement afterThrowing() method.
    }

    /**
     * 这个方法好像就只有在 renderJson 的时候才有用
     * @return mixed
     */
    function afterReturning()
    {
        // TODO: Implement afterReturning() method.
    }
}